﻿using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Metadata.Builders;
using Microsoft.Extensions.Configuration;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Workflow_Core_API.Persisted;
using WorkflowCore.Persistence.EntityFramework.Models;
using WorkflowCore.Persistence.EntityFramework.Services;
using WorkflowCore.Service.Dto.Fundamentals;

namespace WorkflowCore.Service.Persisted
{
    public class WorkflowCoreDbcontext : WorkflowDbContext
    {
        #region 获取连接字符串
        protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
        {
            var configuration = new ConfigurationBuilder()
           .SetBasePath(AppContext.BaseDirectory)
           .AddJsonFile("appsettings.json", optional: false, reloadOnChange: true)
           .Build();
            var connectionString = configuration.GetConnectionString("Whq");
            optionsBuilder.UseMySql(connectionString, MySqlServerVersion.LatestSupportedServerVersion);
        }
        #endregion
        #region 设计表
        /// <summary>
        /// 流程定义/模板
        /// </summary>
        public DbSet<PersistedWorkflowDefinition> persistedWorkflowDefinition { get; set; }
        /// <summary>
        /// 流程审批表(代办)
        /// </summary>
        public DbSet<PersistedWorkflowAuditor> persistedWorkflowAuditor { get; set; }
        /// <summary>
        /// 流程业务实例表,与 PersistedWorkflow一对一关系
        /// </summary>
        public DbSet<PersistedWorkflowInstance> persistedWorkflowInstance { get; set; }
        /// <summary>
        /// 操作人员名单表
        /// </summary>
        public DbSet<WorkflowCoreUser> WorkflowCoreUser { get; set; }
        #endregion
        #region Workflow引擎自带
        /// <summary>
        /// 流程实例
        /// </summary>
        public DbSet<PersistedWorkflow> PersistedWorkflow { get; set; }
        /// <summary>
        /// 流程执行节点
        /// </summary>
        public DbSet<PersistedExecutionPointer> PersistedExecutionPointer { get; set; }
        /// <summary>0
        /// 流程引擎的错误日志
        /// </summary>
        public DbSet<PersistedExecutionError> PersistedExecutionError { get; set; }
        /// <summary>
        /// 执行节点属性扩展表
        /// </summary>
        public DbSet<PersistedExtensionAttribute> PersistedExtensionAttribute { get; set; }
        /// <summary>
        /// WaitFor等待事件订阅表
        /// </summary>
        public DbSet<PersistedSubscription> PersistedSubscription { get; set; }
        /// <summary>
        /// 事件发布表
        /// </summary>
        public DbSet<PersistedEvent> PersistedEvent { get; set; }
        /// <summary>
        /// 执行计划
        /// </summary>
        public DbSet<PersistedScheduledCommand> PersistedScheduledCommand { get; set; }
        #endregion
        #region 实现Workflow引擎自带抽象类
        protected override void ConfigureEventStorage(EntityTypeBuilder<PersistedEvent> builder)
        {
            builder.ToTable("Event");
        }

        protected override void ConfigureExecutionErrorStorage(EntityTypeBuilder<PersistedExecutionError> builder)
        {
            builder.ToTable("ExecutionError");
        }

        protected override void ConfigureExecutionPointerStorage(EntityTypeBuilder<PersistedExecutionPointer> builder)
        {
            builder.ToTable("ExecutionPointer");
        }

        protected override void ConfigureExetensionAttributeStorage(EntityTypeBuilder<PersistedExtensionAttribute> builder)
        {
            builder.ToTable("ExtensionAttribute");
        }

        protected override void ConfigureScheduledCommandStorage(EntityTypeBuilder<PersistedScheduledCommand> builder)
        {
            builder.ToTable("ScheduledCommand");
        }

        protected override void ConfigureSubscriptionStorage(EntityTypeBuilder<PersistedSubscription> builder)
        {
            builder.ToTable("Subscription");
        }

        protected override void ConfigureWorkflowStorage(EntityTypeBuilder<PersistedWorkflow> builder)
        {
            builder.ToTable("Workflow");
        }
        #endregion
    }
}
